package com.site.service.impl;

import com.common.exception.BusinessException;
import com.site.mapper.UserMapper;
import com.site.model.User;
import com.site.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by robin on 2016/12/22.
 * 管理员 service Impl
 */
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper mapper;

    /**
     * 添加管理员
     * @param user 管理员对象
     * @return 结果
     */
    @Override
    public int save(User user) {
        if(StringUtils.isBlank(user.getUserName())) throw new BusinessException("请输入用户名");
        if(StringUtils.isBlank(user.getUserPassword())) throw new BusinessException("请输入密码");
        user.setTime(new Date());
        return mapper.insertSelective(user);
    }

    /**
     * 修改密码
     * @param id  管理员Id
     * @param pwd 新密码
     * @return 结果
     */
    @Override
    public int updatePwd(String id, String pwd) {
        if(StringUtils.isBlank(id) || StringUtils.isBlank(pwd)) return 0;
        User user = new User();
        user.setUserId(id);
        user.setUserPassword(pwd);
        return mapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User selectByName(String name) {
        User user = new User();
        user.setUserName(name);
        return mapper.selectOne(user);
    }
}
